package com.xiaoma.tpo.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.xiaoma.tpo.baseDao.BaseDaoImpl;
import com.xiaoma.tpo.baseDao.DefaultRowMapper;
import com.xiaoma.tpo.domain.BonusTask;
import com.xiaoma.tpo.domain.BonusTaskRule;
import com.xiaoma.tpo.resource.TPOConstants;

@Repository
public class BonusTaskDao extends BaseDaoImpl<BonusTask>{
    
    @SuppressWarnings("unchecked")
    public List<BonusTaskRule> findRules(int timeType) {
        String sql = "SELECT * FROM bonus_task_rule where time_type = ?";
//        if (timeType == TPOConstants.TASK_FIXED) {
//            sql = "SELECT * FROM bonus_task_rule where time_type = ?";
//        } else {
//            sql = "SELECT * FROM bonus_task_rule where time_type = ? ORDER BY RAND() LIMIT 2";
//        }
        List<?> result = jdbcTemplate.query(sql, new DefaultRowMapper(BonusTaskRule.class, getNameHandler()), timeType);
        return (List<BonusTaskRule>)result;
    }
    
    @SuppressWarnings("unchecked")
    public List<BonusTaskRule> findRulesRandom(int timeType, int count) {
        String sql = "SELECT * FROM bonus_task_rule where time_type = ? ORDER BY RAND() LIMIT ?";
//        if (timeType == TPOConstants.TASK_FIXED) {
//            sql = "SELECT * FROM bonus_task_rule where time_type = ?";
//        } else {
//            sql = "SELECT * FROM bonus_task_rule where time_type = ? ORDER BY RAND() LIMIT ?";
//        }
        List<?> result = jdbcTemplate.query(sql, new DefaultRowMapper(BonusTaskRule.class, getNameHandler()), timeType, count);
        return (List<BonusTaskRule>)result;
    }
    
    @SuppressWarnings("unchecked")
    public List<BonusTask> findTaskByType(int timeType) {
        String sql = "SELECT * FROM bonus_task where time_type = ? and start <= now() and end > now()";
        List<?> result = jdbcTemplate.query(sql, new DefaultRowMapper(BonusTask.class, getNameHandler()), timeType);
        return (List<BonusTask>)result;
    }
    
}
